package com.appspot.battlerafts.classes;

import com.appspot.battlerafts.enums.GameState;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@PersistenceCapable(detachable="true")
public class Game {

    @PrimaryKey
    private String gameID;

    public void setGameState(GameState gameState) {
        this.gameState = gameState;
    }

    public Game(String gameID,Long initator, Integer nrOfPlayers, ArrayList<Long> idendedPlayers, Date startTime) {
        this.gameID = gameID;
        this.initator = initator;
        this.nrOfPlayers = nrOfPlayers;
        this.itendedPlayers = idendedPlayers;
        ArrayList<Long> registered = new ArrayList<Long>();
        registered.add(initator);
        this.registeredPlayers = registered;
        this.startTime = startTime;
        this.gameState = GameState.WAITING;
        if (idendedPlayers.size() == nrOfPlayers) {
            this.gamePrivate = true;
        } else {
            this.gamePrivate = false;
        }
    }

    public ArrayList<Long> getItendedPlayers() {
        return itendedPlayers;
    }

    public String getGameID() {
        return gameID;
    }

    public boolean isGamePrivate() {
        return gamePrivate;
    }

    public GameState getGameState() {
        return gameState;
    }

    public Long getInitator() {
        return initator;
    }

    public Integer getNrOfPlayers() {
        return nrOfPlayers;
    }

    public Date getStartTime() {
        return startTime;
    }

    @Persistent
    private boolean gamePrivate;

    @Persistent
    private Long initator;

    @Persistent
    private GameState gameState;

    @Persistent
    private Integer nrOfPlayers;


    public void setItendedPlayers(ArrayList<Long> itendedPlayers) {
        this.itendedPlayers = itendedPlayers;
    }

    @Persistent(defaultFetchGroup="true")
    private ArrayList<Long> itendedPlayers;

    public ArrayList<Long> getRegisteredPlayers() {
        return registeredPlayers;
    }

    public void setRegisteredPlayers(ArrayList<Long> registeredPlayers) {
        this.registeredPlayers = registeredPlayers;
    }

    @Persistent(defaultFetchGroup="true")
    private ArrayList<Long> registeredPlayers;

    @Persistent
    private Date startTime;

    @Override
    public String toString(){
        StringBuilder s = new StringBuilder("[");
        for (Long l : itendedPlayers){
            s.append(l + " ");
        }
        s.append("]");
        return "ID: " + gameID +" gameState: " + gameState +  " nrOfPlayers: " + nrOfPlayers + " initatorID: " + initator +  " startTime: " + new SimpleDateFormat("yyyy-mm-dd HH:MM:ss").format(startTime);
    }
}
